export const store = new Vuex.Store({
    state: {
        list: [],
        doneList: [],
        indexlist: 0,
        indexDoneList:0,
    },
    mutations: {
        add(state,msg) {
            if (state.list.indexOf(msg) < 0) {
                state.list.push(msg);
                state.indexlist = state.list.length;
            } else {
                alert("输入重复")
            }
        },
        finished(state,index) {
            let it = state.list.splice(index, 1);
            state.doneList.push(it.toString())
            state.indexlist = state.list.length;
            state.indexDoneList = state.doneList.length;
        },
        delClick(state,index) {
            state.list.splice(index, 1);
            state.indexlist = state.list.length;
        },
        finishedClick(state,index) {
            state.doneList.splice(index, 1);
            state.indexDoneList = state.doneList.length;
        },

        // init
        init(state) {
            let todoList = JSON.parse(localStorage.getItem("list"));
            if (todoList) {
                 state.list = todoList;
            }
  
            let doneList = JSON.parse(localStorage.getItem("doneList"));
            if (doneList) {
                 state.doneList = doneList;
            }
            state.indexlist = state.list.length;
            state.indexDoneList = state.doneList.length;
        }
    },
    actions: {
        add(context,msg) {
            context.commit("add",msg)
        },
        finished(context,index) {
           context.commit("finished",index)
        },
        delClick(context,index) {
            context.commit("delClick",index)
        },
        finishedClick(context,index) {
            context.commit("finishedClick",index)
        },
        // 初始化读取localStorage中
        init(context) {
            context.commit("init");
        }
    }
});

